Imports CDM.DataCore.CaseNotes
Imports Cdm.DataCore.Budget
Partial Class Case_ViewComments
    Inherits System.Web.UI.Page

    Private ReadOnly Property ParticipantId() As Integer
        Get
            Return EntityController.GetParticipant().ParticipantId
        End Get
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CommentsGridView.Visible = True



        If Not Me.IsPostBack Then
            BindComments()
        End If
    End Sub

    Private Sub BindComments()
        If Not Request("participantdocumentid") Is Nothing Then
            BindParticipantDocumentComments()
        End If

        If Not Request("spendingplanid") Is Nothing Then
            BindSpendingPlanComments()
        End If
    End Sub

    Private Sub BindParticipantDocumentComments()
        Dim pd As ParticipantDocument = ParticipantDocument.Retrieve(CInt(Request("participantdocumentid")))
        PageDescriptionLabel.Text = "Participant Document Comments"
        RecordDescriptionLabel.Text = pd.Description
        CommentsLabel.Text = pd.Comments
    End Sub

    Private Sub BindSpendingPlanComments()
        Dim sp As SpendingPlan = SpendingPlan.Retrieve(CInt(Request("spendingplanid")))
        PageDescriptionLabel.Text = "Spending Plan Comments"
        RecordDescriptionLabel.Text = "Spending Plan starting " + sp.StartDate().ToShortDateString()
        CommentsLabel.Text = sp.Comments

        Dim participantId = Me.ParticipantId
        Dim refId = sp.SpendingPlanId
        Dim caseNoteTypeId As Integer = 17008
        Dim spendPlanComment As String = String.Concat("Spending Plan ", sp.StartDate.ToShortDateString(), " - ", sp.EndDate.ToShortDateString())

        Dim SPcaseNote As New CaseNote
        SPcaseNote = CaseNote.ListByNoteTypeAndReference(participantId, refId, caseNoteTypeId, spendPlanComment, UserAuthorization.CurrentUser.UserId)
        CommentsLabel.Text = SPcaseNote.Note

        Dim caseNoteId As Integer = SPcaseNote.CaseNoteId
        Dim caseNoteResponses = CaseNoteResponseDisplay.ListByCaseNote(caseNoteId)

        If caseNoteResponses.ToString() <> "" Then
            CommentsGridView.DataSource = caseNoteResponses
            CommentsGridView.DataBind()
        End If


    End Sub

    Protected Sub PostReponseButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PostReponseButton.Click
        Dim sp As SpendingPlan = SpendingPlan.Retrieve(CInt(Request("spendingplanid")))

        Dim participantId = Me.ParticipantId
        Dim refId = sp.SpendingPlanId
        Dim caseNoteTypeId As Integer = 17008
        Dim caseNote As New CaseNote

        caseNote = caseNote.ListByNoteTypeAndReference(participantId, refId, caseNoteTypeId, "Spending Plan Comment", UserAuthorization.CurrentUser.UserId)

        Dim tran As New Gentle.Framework.Transaction
        Dim caseNoteResponse As New CaseNoteResponse
        caseNoteResponse.CaseNoteId = caseNote.CaseNoteId
        caseNoteResponse.LastModified = DateTime.Today.ToString()
        caseNoteResponse.DateModified = DateTime.Now
        caseNoteResponse.AuthorUserId = UserAuthorization.CurrentUser.UserId
        caseNoteResponse.Note = ResponseTextBox.Text
        caseNoteResponse.Persist(tran)
        tran.Commit()
        BindComments()
    End Sub
End Class
